package com.doupi.partner.op.controller;

import com.doupi.partner.constant.PartnerTypeEnum;
import com.doupi.partner.op.dto.echarts.*;
import com.doupi.partner.op.util.OpUtils;
import com.doupi.partner.service.ProfitService;
import com.doupi.partner.service.dto.AdministratorDto;
import com.doupi.partner.util.AjaxResponse;
import com.doupi.partner.util.AssertUtils;
import com.doupi.partner.util.RuntimeBusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据浏览
 */
@Controller
@RequestMapping("chartData")
public class ChartDataController {

    private final ProfitService profitService;

    @Autowired
    public ChartDataController(ProfitService profitService) {
        this.profitService = profitService;
    }

    @ResponseBody
    @RequestMapping(value = "topStatistics")
    public AjaxResponse topStatistics() {
        AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();
        Long administratorId = administratorDto.getId();
        if (administratorDto.isPartner() && administratorDto.getPartnerRelation().getType() == PartnerTypeEnum.HEADQUARTERS.getCode()) {
            administratorId = 0L;
        }
        return new AjaxResponse(profitService.topStatistics(administratorId));
    }

    /**
     * 今日实时收益 小时累计收益
     */
    @ResponseBody
    @RequestMapping(value = "accumulateStatisticsGroupByHour")
    public AjaxResponse accumulateStatisticsGroupByHour(Integer beginDay, Integer endDay) {
        AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();
        Long administratorId = administratorDto.getId();
        if (administratorDto.isPartner() && administratorDto.getPartnerRelation().getType() == PartnerTypeEnum.HEADQUARTERS.getCode()) {
            administratorId = 0L;
        }

        // x轴数据
        List<AxisData> xAxisData = new ArrayList<>();
        // 系列数据
        List<SeriesData> seriesData = new ArrayList<>();
        // 数据
        List<Map<String, Object>> list = profitService.accumulateStatisticsGroupByHour(administratorId, beginDay, endDay);

        list.forEach(item -> {
            AxisData xAxis = new AxisData();
            xAxis.setValue(item.get("date").toString());
            xAxisData.add(xAxis);

            SeriesData series = new SeriesData();
            series.setValue(((BigDecimal) item.get("amount")).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());

            Label label = new Label();
            Normal normal = new Normal();
            normal.setPosition("top");
            label.setNormal(normal);

            series.setLabel(label);
            seriesData.add(series);
        });
        // 标题
        Title title = new Title();
        title.setText("今日实时收益");
        title.setLeft("center");
        title.setTop("top");

        // y轴数据
        List<Axis> yAxisList = new ArrayList<>();
        Axis yAxis = new Axis();
        yAxis.setType(AxisType.VALUE.getName());
        yAxis.setName("元");
        yAxisList.add(yAxis);

        // x轴数据
        List<Axis> xAxisList = new ArrayList<>();
        Axis xAxis = new Axis();
        xAxis.setData(xAxisData);
        xAxis.setName("时间");
        xAxisList.add(xAxis);

        // 系列列表
        List<Series> seriesList = new ArrayList<>();
        Series series = new LineSeries();
        series.setName("收益");
        series.setData(seriesData);
        seriesList.add(series);

        return new AjaxResponse(new EchartsData(title, xAxisList, yAxisList, seriesList));
    }

    /**
     * 历史收益
     */
    @ResponseBody
    @RequestMapping(value = "historyStatisticsGroupByHourOrDate")
    public AjaxResponse historyStatisticsGroupByHourOrDate(Integer type, Integer beginDay, Integer endDay) {
        AdministratorDto administratorDto = OpUtils.getCurrentLoginAdministrator();
        Long administratorId = administratorDto.getId();
        if (administratorDto.isPartner() && administratorDto.getPartnerRelation().getType() == PartnerTypeEnum.HEADQUARTERS.getCode()) {
            administratorId = 0L;
        }

        // x轴数据
        List<AxisData> xAxisData = new ArrayList<>();
        // 系列数据
        List<SeriesData> seriesDataList = new ArrayList<>();

        // x轴数据
        List<Axis> xAxisList = new ArrayList<>();
        Axis xAxis = new Axis();
        xAxis.setData(xAxisData);

        xAxisList.add(xAxis);

        // 数据
        List<Map<String, Object>> list;
        AssertUtils.assertNotNull(type, "数据类型不存在");
        Series series;
        switch (type) {
            case 1:// 天
                list = profitService.historyStatisticsGroupByDate(administratorId, beginDay, endDay);
                series = new LineSeries();
                series.setName("收益");
                xAxis.setName("日期");
                break;
            case 2:// 小时
                list = profitService.historyStatisticsGroupByHour(administratorId, beginDay, endDay);
                series = new BarSeries();
                series.setName("收益");
                xAxis.setName("时间");
                break;
            default:
                throw RuntimeBusinessException.createException("查询类型不正确");
        }

        list.forEach(item -> {
            AxisData axisData = new AxisData();
            axisData.setValue(item.get("date").toString());
            xAxisData.add(axisData);

            SeriesData seriesData = new SeriesData();
            seriesData.setValue(((BigDecimal) item.get("amount")).setScale(2, BigDecimal.ROUND_HALF_EVEN).toString());

            Label label = new Label();
            Normal normal = new Normal();
            normal.setPosition("top");
            label.setNormal(normal);

            seriesData.setLabel(label);
            seriesDataList.add(seriesData);
        });

        // 标题
        Title title = new Title();
        title.setText("历史收益");
        title.setLeft("center");
        title.setTop("top");

        // y轴数据
        List<Axis> yAxisList = new ArrayList<>();
        Axis yAxis = new Axis();
        yAxis.setType(AxisType.VALUE.getName());
        yAxis.setName("元");
        yAxisList.add(yAxis);

        // 系列列表
        List<Series> seriesList = new ArrayList<>();

        series.setData(seriesDataList);
        seriesList.add(series);

        return new AjaxResponse(new EchartsData(title, xAxisList, yAxisList, seriesList));
    }

    /**
     * 昨日收益排名
     */
    @ResponseBody
    @RequestMapping(value = "yesterdayProfitRank/{top}")
    public AjaxResponse yesterdayProfitRank(@PathVariable("top") Integer top) {
        return new AjaxResponse(profitService.queryYesterdayProfitRank(top));
    }
}
